package eval;

import network.Device;
import network.Network;
import network.Rule;
import networkLoader.FabricNetwork;
import verifier.Ports.PersistentPorts;
import verifier.util.Changes;
import verifier.JiffyVerifier;

import java.util.ArrayList;
import java.util.Collections;

public class FBEval {
    public static void main(String[] args) {
        Network network = FabricNetwork.getNetwork(112);
        System.out.println(network.getAllDevices().size());

        Device.cnt = 0;
        network.getAllDevices().forEach(device -> device.uid = Device.cnt++);

        seq(network, 8, 1, false);
        seq(network, 8, 1, true);
    }

    private static void seq(Network network, int prefix, int npod, boolean useFFMT) {
        for (int k = 0; k < npod; k ++) {
            System.gc();

            ArrayList<Rule> rules = new ArrayList<>();
            for (Rule rule : network.getInitialRules()) {
                if ((rule.getMatch().longValue() >> (32 - prefix)) == k) {
                    rules.add(new Rule(rule.getDevice(), rule.getMatch().longValue() & ((1L << (32 - prefix)) - 1),
                            Math.max(0, rule.getPrefix() - prefix), rule.getOutPort()));
                }
            }

            JiffyVerifier verifier = new JiffyVerifier(network, 32 - prefix, new PersistentPorts());
            if (useFFMT) {
                Changes changes = verifier.insertMiniBatch(rules);
                verifier.update(changes);
            } else {
                for (Rule rule : rules) {
                    Changes changes = verifier.insertMiniBatch(new ArrayList<>(Collections.singletonList(rule)));
                    verifier.update(changes);
                }
            }
            System.out.println("useFFMT: " + useFFMT);
            System.out.println("#EC: " + verifier.predSize());
            verifier.printTime(network.getInitialRules().size());
        }
    }
}
